﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using WebSimplicity.Data.Helpers;
using System.Web.Security;

namespace WebSimplicity.Data.Blog
{
    public interface IBlogPost
    {
        int PostId { get; set; }

        [Display(Name = "WebsiteId")]
        Guid WebsiteId { get; set; }

        [Display(Name = "Post Slug")]
        string Slug { get; set; }

        [Display(Name = "Post Title")]
        string Title { get; set; }

        [Display(Name = "Post Excerpt")]
        string PostExcerpt { get; set; }

        [Display(Name = "Post Content")]
        [AllowHtml]
        string PostContent { get; set; }

        [Display(Name = "Created By")]
        Guid CreatedBy { get; set; }

        [Display(Name = "Created On")]
        DateTime CreatedOn { get; set; }

        [Display(Name = "Modified By")]
        Guid ModifiedBy { get; set; }

        [Display(Name = "Last Modified")]
        DateTime ModifiedOn { get; set; }

        [Display(Name = "Published")]
        bool Published { get; set; }

        [Display(Name = "Allow Comments")]
        bool AllowComments { get; set; }

        [Display(Name = "Publish Date")]
        DateTime PublishDate { get; set; }
    }

    [MetadataType(typeof(IBlogPost))]
    partial class ws_Blog_Posts : IBlogPost { }

    public static class Posts
    {
        static Website web = new Website().Current();
        static MembershipUser user = Membership.GetUser();

        public static IEnumerable<ws_Blog_Posts> GetPosts()
        {
            Entities db = new Entities(Config.GetConnection("Blog.Data"));
            return db.ws_Blog_Posts.Where(i => i.WebsiteId == web.WebsiteId);
        }

        public static ws_Blog_Posts GetPost(int PostId)
        {
            Entities db = new Entities(Config.GetConnection("Blog.Data"));
            return db.ws_Blog_Posts.Single(i => i.PostId == PostId);
        }
        public static int CreatePost(ws_Blog_Posts post,FormCollection form)
        {
            Entities db = new Entities(Config.GetConnection("Blog.Data"));
            post.CreatedBy = (Guid)user.ProviderUserKey;
            post.CreatedOn = DateTime.Now;
            post.WebsiteId = web.WebsiteId;
            post.ModifiedBy = (Guid)user.ProviderUserKey;
            post.ModifiedOn = DateTime.Now;
            db.ws_Blog_Posts.AddObject(post);
            db.SaveChanges();
            foreach (string category in form.Keys)
            {
                if (category.IndexOf("cat_") == 0)
                {
                    ws_Blog_Category_Map bcm = new ws_Blog_Category_Map();
                    bcm.CategoryGuid = ShortGuid.Decode(category.Substring(4));
                    bcm.WebsiteId = web.WebsiteId;
                    bcm.PostId = post.PostId;
                    db.ws_Blog_Category_Map.AddObject(bcm);
                }
                db.SaveChanges();
            }
            return post.PostId;
        }
        public static bool UpdatePost(int id, ws_Blog_Posts pst,FormCollection form)
        {
            Entities db = new Entities(Config.GetConnection("Blog.Data"));
            ws_Blog_Posts post = db.ws_Blog_Posts.Single(i => i.PostId == id);
            post.AllowComments = pst.AllowComments;
            post.ModifiedBy = (Guid)user.ProviderUserKey;
            post.ModifiedOn = DateTime.Now;
            post.PostContent = pst.PostContent;
            post.PostExcerpt = pst.PostExcerpt;
            post.PublishDate = pst.PublishDate;
            post.Published = pst.Published;
            post.Slug = pst.Slug;
            post.Title = pst.Title;
            var categories = form.AllKeys.Where(i => i.IndexOf("cat_") == 0);
            foreach (ws_Blog_Category_Map catmap in post.ws_Blog_Category_Map)
            {
                if (!(categories.Contains(ShortGuid.Encode(catmap.CategoryGuid))))
                {
                    db.ws_Blog_Category_Map.DeleteObject(catmap);
                    db.SaveChanges();
                }
            }
            foreach (string category in categories)
            {
                ws_Blog_Category_Map cat = new ws_Blog_Category_Map();
                cat.WebsiteId = web.WebsiteId;
                cat.CategoryGuid = ShortGuid.Decode(category.Substring(4));
                cat.PostId = post.PostId;
                if (!post.ws_Blog_Category_Map.Contains(cat))
                {
                    db.ws_Blog_Category_Map.AddObject(cat);
                    db.SaveChanges();
                }
            }
            db.SaveChanges();
            return true;
        }
    }
}
